package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.vo.CustomerVo;
import com.ruoyi.system.domain.vo.StoreVo;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysStoreService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.domain.SysCustomer;
import com.ruoyi.system.service.ISysCustomerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.poi.ExcelUtil;


/**
 * 经销商档案Controller
 *
 * @author anan
 * @date 2021-03-02
 */
@RestController
@RequestMapping("/base/customer")
public class SysCustomerController extends BaseController
{
    @Autowired
    private ISysCustomerService customerService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysStoreService storeService;

    /**
     * 查询经销商档案列表
     */
    @PreAuthorize("@ss.hasPermi('base:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerVo customerVo)
    {
		PagePlus<CustomerVo, CustomerVo> customerVoCustomerVoPagePlus = customerService.pageVo(PageUtils.buildPagePlus(), CustomerVo.class);
		return PageUtils.buildDataInfo(customerVoCustomerVoPagePlus);
    }

    /**
     * 导出经销商档案列表
     */
    @PreAuthorize("@ss.hasPermi('base:customer:export')")
    @Log(title = "经销商档案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysCustomer sysCustomer)
    {
        List<SysCustomer> list = customerService.selectSysCustomerList(sysCustomer);
        ExcelUtil<SysCustomer> util = new ExcelUtil<SysCustomer>(SysCustomer.class);
        return util.exportExcel(list, "customer");
    }

    /**
     * 获取经销商档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerService.getById(id));
    }

    /**
     * 新增经销商档案
     */
    @PreAuthorize("@ss.hasPermi('base:customer:add')")
    @Log(title = "经销商档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerVo customerVo)
    {
        return toAjax(customerService.save(customerVo));
    }

    /**
     * 修改经销商档案
     */
    @PreAuthorize("@ss.hasPermi('base:customer:edit')")
    @Log(title = "经销商档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerVo customerVo)
    {
        return toAjax(customerService.updateById(customerVo));
    }

    /**
     * 删除经销商档案
     */
    @PreAuthorize("@ss.hasPermi('base:customer:remove')")
    @Log(title = "经销商档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
		List list = CollectionUtils.arrayToList(ids);
        return toAjax(customerService.removeByIds(list));
    }

    @GetMapping("/roleCustomerTreeselect/{roleId}")
    public AjaxResult roleCustomerTreeselect(@PathVariable("roleId") Long roleId)
    {
        /** 获取全部经销商 */
		List<CustomerVo> customers = customerService.list();
        /** 获取经销商全部数据 */
        List<Map<String, Object>> maps = customerService.selectCustomerListByRoleId(roleId);
        /** 获取店仓全部数据 */
        List<StoreVo> storeVos = storeService.selectSysStoreByRoleId(roleId);
        /** 转换 */
        List<CustomerVo> customers1 = customerService.conversionList(customers, maps,storeVos);
        AjaxResult ajax = AjaxResult.success();
        SysRole role = roleService.selectRoleById(roleId);
        role.setStoreIds(storeVos);
        ajax.put("role",role);
        ajax.put("customers", customerService.buildCustomerTreeSelect(customers1));
        return ajax;
    }

    @GetMapping("/userCustomerTreeselect/{userId}")
    public AjaxResult userCustomerTreeselect(@PathVariable("userId") Long userId)
    {
        /** 获取全部经销商 */
		List<CustomerVo> customers = customerService.list();
		/** 根据userId获取全部经销商 */
		List<Map<String,Object>> maps = customerService.selectCustomerListByUserId(userId);
        /** 根据userId获取全部店仓 */
        List<StoreVo> storeVos = storeService.selectSysStoreByUserId(userId);
        /** 转换 */
        List<CustomerVo> customers1 = customerService.conversionList(customers, maps,storeVos);
        AjaxResult ajax = AjaxResult.success();
		SysUser user = userService.getById(userId);
        user.setStoreIds(storeVos);
        ajax.put("user",user);
        if (customers.size() > 0) {
            ajax.put("customers", customerService.buildCustomerTreeSelect(customers1));
        }
        return ajax;
    }
}
